package com.example.module_filemanage.MVVM.ViewModule;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.module_filemanage.Adapter.LeftMenu_Adapter;
import com.example.module_filemanage.Control.ExpandableLinearLayout;
import com.example.module_filemanage.Interface.ICallBack;
import com.example.module_filemanage.Interface.IFileInfoOnclickListener;
import com.example.module_filemanage.MVVM.Module.Business.TranModule;
import com.example.module_filemanage.MVVM.Module.Entil.Item_FileInfo_module;
import com.example.module_filemanage.MVVM.Module.Entil.Left_menu;
import com.example.module_filemanage.MVVM.Module.QiNiu.FileManager;
import com.example.module_filemanage.MVVM.View.Dialog.MDialog;
import com.example.module_filemanage.MVVM.View.FMMainActivity;
import com.example.module_filemanage.Public.Data;
import com.example.module_filemanage.R;
import com.example.module_filemanage.Utils.ImageHelper;
import com.example.module_filemanage.Utils.TimeTool;
import com.example.module_filemanage.databinding.FmActivityMainBinding;
import com.example.module_filemanage.databinding.FmItemMain2Binding;
import com.example.module_filemanage.databinding.FmItemMainBinding;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;


/**
 * Created by SJ on 2018/4/20.
 */

public class FMMain_VM {
    FmActivityMainBinding mBinding;
    Context mContext;
    ProgressDialog mprogressDialog;
    public LeftMenu_Adapter _leftMenu_adapter;
    private ICallBack cakkback;
    public FMMain_VM(Context context, FmActivityMainBinding binding, ProgressDialog progressDialog){
        this.mBinding=binding;
        this.mContext=context;
        this.mprogressDialog=progressDialog;

    }
    public  void init(final String[] account, ICallBack _callback, final IFileInfoOnclickListener IListener){
        //获取七牛网络数据
        this.cakkback=_callback;
        FileManager.getInstance(mContext.getApplicationContext()).init(new ICallBack<Left_menu>() {
            @Override
            public void success(Left_menu item) {
                //Log.e(TAG, "success: "+items.get(0).getSpacename() );
                _leftMenu_adapter=new LeftMenu_Adapter(mContext,item.getItem_left_menu_modules());
                LinearLayoutManager layoutmanager = new LinearLayoutManager(mContext);
                //设置RecyclerView 布局
                mBinding.includeMenu.recSpacemenu.setLayoutManager(layoutmanager);
                mBinding.includeMenu.recSpacemenu.setAdapter(_leftMenu_adapter);
                mBinding.setLeftmenu(item);
                mprogressDialog.dismiss();
                //拿到所有文件信息
//                FilterAdapter _filterAdapter=new FilterAdapter(mContext,Data.getInstance().ALLFILEINFO);
//                mBinding.fmmain.searchBar.setAdapter(_filterAdapter);
                mBinding.fmmain.searchBar.setSuggestions(Data.getInstance().ALLFILEINFO,IListener);
                mprogressDialog=null;
                initmainview(account);

            }
        },mprogressDialog);

    }
    private void initmainview(String[] account){
        TranModule.allSort(account, new ICallBack<int[]>() {
            @Override
            public void success(int[] items) {

                myHandler.sendEmptyMessage(0);
            }
        });
    }

    //主线程
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch(msg.what) {
                case 0:
                    mBinding.fmmain.expandable.removeAllViews();
                    mBinding.fmmain.expandable.init();
                    Calendar date = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
                    int dat = date.get(Calendar.DAY_OF_MONTH) - 5;
                    date.set(Calendar.DAY_OF_MONTH, dat);
                    String riqi = String.valueOf(date.get(date.YEAR)) + "-" + TimeTool.format(date.get(date.MONTH) + 1) + "-" +TimeTool.format(date.get(date.DATE))+" "+TimeTool.format(date.get(date.HOUR))+":"+TimeTool.format(date.get(date.MINUTE));

                    for (int i=0; i <Data.getInstance().ALLFILEINFO.size(); i++) {
                        if (Data.getInstance().ALLFILEINFO.get(i).getFiletime().compareTo(riqi)<0) break;
                        if (Data.getInstance().ALLFILEINFO.get(i).getFilename().matches(".+\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)")) {
                            FmItemMain2Binding _binding=DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fm_item_main2, mBinding.fmmain.expandable, false);
                            _binding.setItem(Data.getInstance().ALLFILEINFO.get(i));
                            _binding.imgDownload.setImageResource(R.drawable.ic_file_download_black_24dp);
                            _binding.imgDel.setImageResource(R.drawable.ic_delete_forever_black_24dp);

                            ImageHelper.load(_binding.imageView4,Data.getInstance().ALLFILEINFO.get(i).getFileurl());
                            mBinding.fmmain.expandable.addItem(_binding.getRoot(),Data.getInstance().ALLFILEINFO.get(i));
                        }else {
                            FmItemMainBinding _binding=DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fm_item_main, mBinding.fmmain.expandable, false);
                            _binding.setItem(Data.getInstance().ALLFILEINFO.get(i));
                            _binding.imgDownload.setImageResource(R.drawable.ic_file_download_black_24dp);
                            _binding.imgDel.setImageResource(R.drawable.ic_delete_forever_black_24dp);
                            mBinding.fmmain.expandable.addItem(_binding.getRoot(),Data.getInstance().ALLFILEINFO.get(i));
                        }
                    }
                    cakkback.success(true);

                    //mBinding.fmmain.llBottom.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    };

}
